package org.cn.lennon.mybatis.session.impl;

import java.lang.reflect.Proxy;
import java.sql.SQLException;

import org.cn.lennon.mybatis.config.Configuration;
import org.cn.lennon.mybatis.executors.Executors;
import org.cn.lennon.mybatis.proxy.MapperProxy;
import org.cn.lennon.mybatis.session.SqlSession;
import org.cn.lennon.mybatis.transcation.Transaction;

public class DefaultSqlSession implements SqlSession {

	private Configuration configuration;
	private Executors executors;

	public DefaultSqlSession(Configuration configuration, Executors executors) {
		this.configuration = configuration;
		this.executors = executors;
	}

	public <T> T selectOne(String statmenId, Object[] args) throws SQLException {
		return executors.query(configuration.mapperRegister.getSqlMapperStatment(statmenId), args);
	}

	public <T> T getMapper(Class<T> clazz) {
		return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz },
				new MapperProxy(this, configuration));
	}

	public void commit() throws SQLException {
		executors.commit();
	}

	public void rollback() throws SQLException {
		executors.rollback();
	}

	public void close() throws SQLException {
		executors.close();
	}

	public int update(String statmenId, Object[] args) throws SQLException {
		return executors.update(configuration.mapperRegister.getSqlMapperStatment(statmenId), args);
	}
	
	public int insert(String statmenId, Object[] args) throws SQLException {
		return executors.insert(configuration.mapperRegister.getSqlMapperStatment(statmenId), args);
	}
	
	public int delete(String statmenId, Object[] args) throws SQLException {
		return executors.delete(configuration.mapperRegister.getSqlMapperStatment(statmenId), args);
	}
	
	
}
